<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Category extends MY_Luckydog {
    public function __construct()
    {

        parent::__construct();
        $this->load->model('luckydog/category_model', 'cat');
    }
    public function index()
    {
         $_SESSION['v_menu'] = 'category';
        $rs = $this->cat->get_cat_list(3);
        infiniteAssort($rs,0,0,"parent_id","cat_id",$result,"p_level");
        foreach ($result as $key => $value) {
            if($value['parent_id']){
                //$top_info = explode(',',$this->cat->get_level_top_bycid($value['parent_id']));
                //$result[$key]['top_info'] = $top_info[0].'-';
            }else{
                $result[$key]['top_info'] = $value['cat_id'];
            }
            
        }
        $data=array(
            'cat_list'=>$result,
        );

        $this->load->view('luckydog/item/category_list',$data);
    }
    //添加分类 html
    public function add()
    {
        $top_cat = $this->cat->get_cat_list();
        $rs = $this->cat->get_cat_list();
        infiniteAssort($top_cat,0,0,"parent_id","cat_id",$result,"p_level");
       
        $data=array(
            'cat_list'=>$result,
            
        );
        $this->load->view('luckydog/item/category_add',$data);
    }
    //修改分类 html
    public function edit()
    {
        $id  = $this->input->get("id");
        if (empty($id)){
            show_message("参数错误~, <a class='poi' onclick='javascript:history.go(-1);'>返回>></a>");
            exit;
        }
        $top_cat = $this->cat->get_cat_list();
        $rs = $this->cat->get_cat_list();
        infiniteAssort($top_cat,0,0,"parent_id","cat_id",$result,"p_level",1,array('cat_id'=>$id));

        $cat_row = $this->cat->get_cat_by_id($id);
        $data=array(
            'cat_list'=>$result,
            'cat_row'=>$cat_row
        );
        $this->load->view('luckydog/item/category_edit',$data);
    }
    public function do_add(){
        $parent_id  = $this->input->post("parent_id");
        $cat_name   = $this->input->post("cat_name");
        $sort_order = $this->input->post("sort_order");
        $p_level = $this->input->post("next_level");
        $cover = $this->input->post("cover");

        $data = array(
            "parent_id"=>$parent_id,
            "cat_name"=>$cat_name,
            "sort_order"=>$sort_order,
            "cover"=>$cover,
            "p_level"=>intval($p_level),
        );

        $insert_id = $this->cat->add_cat($data);
        if($p_level == 2){
            $importData = array(
                "parent_id"=>$insert_id,
                "cat_name"=>'进口',
                "p_level"=>3,
            );
            $this->cat->add_cat($importData);
            $exportData = array(
                "parent_id"=>$insert_id,
                "cat_name"=>'国产',
                "p_level"=>3,
            );
            $this->cat->add_cat($exportData);

        }
        if($insert_id){
            echo json_encode(array('status'=>1,'message'=>'添加分类成功'));
        }else{
            echo json_encode(array('status'=>-1,'message'=>'添加分类失败'));
        }
        exit;
    }
    public function do_edit(){
        $cat_id   = $this->input->post("cat_id");
        $cat_name   = $this->input->post("cat_name");
        $sort_order = $this->input->post("sort_order");
        $cover = $this->input->post("cover");
        $p_level = $this->input->post("next_level");

        $data = array(
            "cat_name"=>$cat_name,
            "sort_order"=>$sort_order,
            "cover"=>$cover,
            "p_level"=>$p_level,
        );
        $edit_flag = $this->cat->edit_cat($cat_id,$data);

        if($edit_flag){
            echo json_encode(array('status'=>1,'message'=>'修改分类成功'));
        }else{
            echo json_encode(array('status'=>-1,'message'=>'修改分类失败'));
        }
        exit;
    }
    public function do_del(){
        $cat_id  = $this->input->post("cat_id");
        $this->cat->del_cat($cat_id);
        echo json_encode(array('status'=>1));
        exit;
    }

    public function set_isrecommend(){
        $isrecommend  = $this->input->post("isrecommend");
        $cat_id   = $this->input->post("cat_id");
        $data = array(
            "isrecommend"=>$isrecommend
        );
        $edit_flag = $this->cat->edit_cat($cat_id,$data);
        if($edit_flag){
            echo json_encode(array('status'=>1,'message'=>'修改分类成功'));
        }else{
            echo json_encode(array('status'=>-1,'message'=>'修改分类失败'));
        }
        exit;
    }

    public function customCate(){
        $companyid  = $this->input->get("companyid");
        $cate_list = $this->cat->get_custom_cat_list($companyid);
        $data = array(
            'companyid'=>$companyid,
            'cate_list'=>$cate_list
            );
        $this->load->view('luckydog/item/custom_cate_list',$data);
    }


    //添加分类 html
    public function addCustomCate()
    {
        $top_cat = $this->cat->get_cat_list();
        $companyid  = $this->input->get("companyid");
        $rs = $this->cat->get_cat_list();
        infiniteAssort($top_cat,0,0,"parent_id","cat_id",$result,"p_level");
        $data=array(
            'cat_list'=>$result,
            'companyid'=>$companyid
        );
        $this->load->view('luckydog/item/custom_cate_add',$data);
    }
    //修改分类 html
    public function editCustomCate()
    {
        $id  = $this->input->get("id");
        if (empty($id)){
            show_message("参数错误~, <a class='poi' onclick='javascript:history.go(-1);'>返回>></a>");
            exit;
        }
        $top_cat = $this->cat->get_cat_list();
        $rs = $this->cat->get_cat_list();
        infiniteAssort($top_cat,0,0,"parent_id","cat_id",$result,"p_level",1,array('cat_id'=>$id));

        $cat_row = $this->cat->get_custom_cat_by_id($id);
        $data=array(
            'cat_list'=>$result,
            'cat_row'=>$cat_row
        );
        $this->load->view('luckydog/item/custom_cate_edit',$data);
    }
    public function do_addCustomCate(){
        $cat_name   = $this->input->post("cat_name");
        $companyid  = $this->input->post("companyid");

        $data = array(
            "cat_name"=>$cat_name,
            "companyid"=>$companyid
        );

        $insert_id = $this->cat->add_CustomCat($data);
        if($insert_id){
            echo json_encode(array('status'=>1,'message'=>'添加分类成功'));
        }else{
            echo json_encode(array('status'=>-1,'message'=>'添加分类失败'));
        }
        exit;
    }
    public function do_editCustomCate(){
        $cat_id   = $this->input->post("cat_id");
        $cat_name   = $this->input->post("cat_name");

        $data = array(
            "cat_map_id"=>$cat_map_id,
            "cat_name"=>$cat_name,
        );
        $edit_flag = $this->cat->edit_CustomCat($cat_id,$data);

        if($edit_flag){
            echo json_encode(array('status'=>1,'message'=>'修改分类成功'));
        }else{
            echo json_encode(array('status'=>-1,'message'=>'修改分类失败'));
        }
        exit;
    }

}

/* End of file category.php */
/* Location: ./application/controllers/luckydog/category.php */